﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Entity.AdminFinancial;
using studentSubsidy.Entity.Apply;
using studentSubsidy.Entity.Refund;
using studentSubsidy.Models;
using studentSubsidy.Utils;

namespace studentSubsidy.Service
{
    public class AdminFinancialService
    {
        private AdminFinancialDao _adminFinancialDao = new AdminFinancialDao();
        private ApplyDao _applyDao = new ApplyDao();

        public Result getPaymentList(string role)
        {
            List<Apply> applies = _adminFinancialDao.getCheckedApply(role);
            List<AllApplyReturn> paymentReturn = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                Project project = _applyDao.getProjectById(applies[i].project_id);
                User user = _applyDao.getUser(applies[i].user_id);
                if (project.isRefund == 1 && applies[i].isSigned != 1)
                {
                    continue;
                }

                var applyReturn = new AllApplyReturn
                {
                    applyId = applies[i].apply_id,
                    applyTime = applies[i].apply_time.Replace("T", " "),
                    projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                    address = user.address,
                    name = user.name,
                    major = user.major,
                    phone = user.phone
                };
                paymentReturn.Add(applyReturn);
            }

            return new Result(200, "搜索成功", paymentReturn, null);
        }

        public Result GetPaymentInfo(int applyId)
        {
            Apply apply = _applyDao.getApplyByApplyId(applyId);
            User user = _applyDao.getUser(apply.user_id);
            Project project = _applyDao.getProjectById(apply.project_id);
            PaymentInfoReturn paymentInfoReturn = new PaymentInfoReturn
            {
                project_name = project.product_name,
                name = user.name,
                bankNumber = apply.bank_number,
                major = user.major,
                phone = user.phone,
                address = user.address,
                enrollment = user.enrollment,
                price = project.product_price,
                isRefund = false
            };
            if (project.isRefund == 1)
            {
                paymentInfoReturn.isRefund = true;
            }

            return new Result(200, "获取成功", paymentInfoReturn, null);
        }

        public Result putPayment(int applyId)
        {
            Apply apply = _applyDao.getApplyByApplyId(applyId);
            Project project = _applyDao.getProjectById(apply.project_id);
            User user = _applyDao.getUser(apply.user_id);

            Payment payment = new Payment
            {
                id = GenerateID.GenForInt(),
                apply_id = applyId,
                user_id = apply.user_id,
                project_id = apply.project_id,
                price = project.product_price,
                isRefund = 0,
                needRefund = project.isRefund
            };
            if (project.isRefund == 1)
            {
                Refund refund = new Refund
                {
                    id = GenerateID.GenForInt(),
                    apply_id = applyId,
                    cur_price = project.product_price,
                    project_id = project.product_id,
                    refund_time = Convert.ToDateTime(user.enrollment).AddYears(4).ToString("s"),
                    sum_price = project.product_price,
                    user_id = apply.user_id,
                    state = 0
                };
                DateTime refund_start = Convert.ToDateTime(refund.refund_time);
                for (int i = 0; i < 12; i++)
                {
                    Refund_Log refundLog = new Refund_Log
                    {
                        id = GenerateID.GenForInt(),
                        refund_id = refund.id,
                        refund_price = Math.Round((double) refund.sum_price / 12, 2),
                        hasRefund = 0,
                        refund_time = refund_start.AddMonths(i).ToString("s")
                    };
                    _adminFinancialDao.putRefundLog(refundLog);
                }

                _adminFinancialDao.putRefund(refund);
            }

            _adminFinancialDao.putPayment(payment);
            return new Result(200, "发放成功", null, null);
        }

        public Result getHasPaymentList(string role)
        {
            List<Apply> applies = _adminFinancialDao.getHasPaymentApply(role);
            List<AllApplyReturn> paymentReturn = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                Project project = _applyDao.getProjectById(applies[i].project_id);
                User user = _applyDao.getUser(applies[i].user_id);
                var applyReturn = new AllApplyReturn
                {
                    applyId = applies[i].apply_id,
                    applyTime = applies[i].apply_time.Replace("T", " "),
                    projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                    address = user.address,
                    name = user.name,
                    major = user.major,
                    phone = user.phone
                };
                paymentReturn.Add(applyReturn);
            }

            return new Result(200, "搜索成功", paymentReturn, null);
        }

        public Result getRefundList(string role)
        {
            List<Apply> applies = _adminFinancialDao.getRefundRoleList(role);
            List<AllApplyReturn> paymentReturn = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                Project project = _applyDao.getProjectById(applies[i].project_id);
                User user = _applyDao.getUser(applies[i].user_id);
                Refund refund = _adminFinancialDao.getRefundList(applies[i].apply_id);
                if (refund.state == 1)
                {
                    continue;
                }
                var applyReturn = new AllApplyReturn
                {
                    applyId = applies[i].apply_id,
                    applyTime = applies[i].apply_time.Replace("T", " "),
                    projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                    name = user.name,
                    address = "还款已启动",
                    major = user.major,
                    phone = user.phone
                };
                if (refund.state == 0)
                {
                    applyReturn.address = "还款未启动";
                }
                if (refund.state == 1)
                {
                    applyReturn.address = "还款已完成";
                }

                paymentReturn.Add(applyReturn);
            }

            return new Result(200, "搜索成功", paymentReturn, null);
        }

        public Result getRefundData(int applyId)
        {
            Refund refund = _adminFinancialDao.getRefundByApplyId(applyId);
            List<EChartData> eChartDatas = new List<EChartData>();
            EChartData NotRefund = new EChartData
            {
                value = refund.cur_price,
                name = "未还款"
            };
            
            EChartData HasRefund = new EChartData
            {
                value = Math.Round(refund.sum_price - refund.cur_price,2),
                name = "已还款"
            };
            eChartDatas.Add(NotRefund);
            eChartDatas.Add(HasRefund);
            return new Result(200, "搜索成功", eChartDatas, null);
        }

        public Result getRefundLogList(int applyId)
        {
            Refund refund = _adminFinancialDao.getRefundByApplyId(applyId);
            User user = _applyDao.getUser(refund.user_id);
            Project project = _applyDao.getProjectById(refund.project_id);
            List<Refund_Log> refundLogs = _adminFinancialDao.getRefundLogByRefundId(refund.id);
            List<RefundLogReturn> refundLogReturns = new List<RefundLogReturn>();

            foreach (var VARIABLE in refundLogs)
            {
                RefundLogReturn refundLogReturn = new RefundLogReturn
                {
                    name = user.name,
                    projectName = project.product_name,
                    refundId = VARIABLE.id,
                    refundPrice = VARIABLE.refund_price,
                    refundTime = VARIABLE.refund_time.Split("T")[0],
                    state = "未偿还"
                };
                if (VARIABLE.hasRefund == 1)
                {
                    refundLogReturn.state = "已偿还";
                }

                if (VARIABLE.hasRefund == -1)
                {
                    refundLogReturn.state = "已逾期";
                }

                refundLogReturns.Add(refundLogReturn);
            }

            refundLogReturns.OrderBy(p => Convert.ToDateTime(p.refundTime));

            return new Result(200, "获取成功", refundLogReturns, null);
        }

        public Result getRefundInfo(int applyId)
        {
            Apply apply = _applyDao.getApplyByApplyId(applyId);
            Refund refund = _adminFinancialDao.getRefundByApplyId(applyId);
            Project project = _applyDao.getProjectById(apply.project_id);
            User user = _applyDao.getUser(apply.user_id);
            RefundInfoReturn refundInfoReturn = new RefundInfoReturn
            {
                project_name = project.product_name,
                username = user.name,
                refund_start = refund.refund_time.Split("T")[0],
                refund_state = "还款已启动",
                useraddress = user.address,
                userphone = user.phone
            };
            if (refund.state == 0)
            {
                refundInfoReturn.refund_state = "还款未启动";
            }
            if (refund.state == 1)
            {
                refundInfoReturn.refund_state = "还款已完成";
            }

            return new Result(200, "获取成功", refundInfoReturn, null);
        }

        public Result updateRefundState(int applyId)
        {
            Refund refund = _adminFinancialDao.getRefundByApplyId(applyId);
            DateTime startTime = Convert.ToDateTime(refund.refund_time).AddMonths(-1);
            if (startTime < DateTime.Now)
            {
                refund.state = 2;
            }

            _adminFinancialDao.updateRefundState(refund);

            List<Refund_Log> refundLogs = _adminFinancialDao.getRefundLogByRefundId(refund.id);
            foreach (var VARIABLE in refundLogs)
            {
                DateTime refundTime = Convert.ToDateTime(VARIABLE.refund_time);

                if (refundTime < DateTime.Now && VARIABLE.hasRefund == 0)
                {
                    VARIABLE.hasRefund = -1;
                }

                _adminFinancialDao.updateRefundLog(VARIABLE);
            }

            return new Result(200, "更新成功", "", "");
        }

        public Result getFinishProjectList(string role)
        {
            List<Apply> applies = _adminFinancialDao.getRefundRoleList(role);
            List<AllApplyReturn> paymentReturn = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                Project project = _applyDao.getProjectById(applies[i].project_id);
                User user = _applyDao.getUser(applies[i].user_id);
                Refund refund = _adminFinancialDao.getRefundList(applies[i].apply_id);
                if (refund.state != 1)
                {
                    continue;
                }
                var applyReturn = new AllApplyReturn
                {
                    applyId = applies[i].apply_id,
                    applyTime = applies[i].apply_time.Replace("T", " "),
                    projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                    name = user.name,
                    address = "还款已启动",
                    major = user.major,
                    phone = user.phone
                };
                if (refund.state == 0)
                {
                    applyReturn.address = "还款未启动";
                }
                if (refund.state == 1)
                {
                    applyReturn.address = "还款已完成";
                }

                paymentReturn.Add(applyReturn);
            }

            return new Result(200, "搜索成功", paymentReturn, null);
        }
    }
}